//
//  EGTimeCell.m
//  Orchard
//
//  Created by ios001 on 16/8/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGTimeCell.h"

@interface EGTimeCell()<UITextFieldDelegate>

@property (nonatomic,strong) UIView      *timeView;

@property (nonatomic,strong) UIButton    *iconBtn;

@property (nonatomic,strong) UIButton    *timeBtn;
//@property (nonatomic,strong)

@property (nonatomic,strong) UILabel     *ReceivingTimeLabel;

@property (nonatomic,strong) UILabel     *remarksLabel;

@property (nonatomic,strong) UITextField *remarksText;

@property (nonatomic,strong) UIButton    *editBtn;

@property (nonatomic,strong) UIImageView *IndicatorImg;

@end
@implementation EGTimeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    [self.contentView addSubview:self.timeView];
    [self.timeView addSubview:self.editBtn];
    [self.timeView addSubview:self.iconBtn];
    [self.timeView addSubview:self.timeBtn];
    [self.timeView addSubview:self.remarksText];
    [self.timeView addSubview:self.IndicatorImg];
    self.remarksText.delegate = self;
}

- (void)layoutSubviews{
//    self.backgroundColor = [UIColor redColor];

    [self setLayout];
 }

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//    if (textField == self.remarksText) {
//        if (textField.text.length > 20)
//        return NO;
//    }
//    
//    return YES;
    
    if (textField == self.remarksText) {
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        if (existedLength - selectedLength + replaceLength > 100) {
            return NO;
        }
    }
    
    return YES;
}

- (void)setLayout{
    [self.timeView makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.leading.trailing.equalTo(self.contentView);
    }];
    
    [self.editBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeView);
        make.trailing.equalTo(self.IndicatorImg).offset(-20);
    }];

    [self.iconBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeView).offset(5);
        make.leading.equalTo(self.timeView).offset(10);
    }];
    
    [self.timeBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconBtn).offset(25);
        make.leading.equalTo(self.timeView).offset(10);
    }];
    
    [self.remarksText makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.trailing.equalTo(self.timeView).offset(-25);
        make.width.offset(250);
    }];
    
    [self.IndicatorImg makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.timeView);
        make.trailing.equalTo(self.timeView).offset(-15);
    }];
}




- (UIButton *)iconBtn{
    if (_iconBtn == nil) {
        _iconBtn = [[UIButton alloc] init];
        [_iconBtn setImage:[UIImage imageNamed:@"icon_lighting"] forState:UIControlStateNormal];
        _iconBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        [_iconBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_iconBtn setTitle:@"  闪电超市" forState:UIControlStateNormal];
    }
    return _iconBtn;
}

- (UIButton *)editBtn{
    if (_editBtn == nil) {
        _editBtn = [[UIButton alloc] init];
        [_editBtn setTitle:@"可预订" forState:UIControlStateNormal];
        [_editBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _editBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _editBtn;
}

- (UIImageView *)IndicatorImg{
    if (_IndicatorImg == nil) {
        _IndicatorImg = [[UIImageView alloc] init];
        _IndicatorImg.image = [UIImage imageNamed:@"icon_go"];
    }
    return _IndicatorImg;
}

- (UIView *)timeView{
    if (_timeView == nil) {
        _timeView = [[UIView alloc] init];
        _timeView.backgroundColor = ThemeColor;
    }
    return _timeView;
}

- (UIButton *)timeBtn{
    if (_timeBtn == nil) {
        _timeBtn = [[UIButton alloc] init];
        [_timeBtn setImage:[UIImage imageNamed:@"v2_notice_dot"] forState:UIControlStateNormal];
//        _timeBtn.imageEdgeInsets = UIEdgeInsetsMake(5, 15, 5, 15);
        _timeBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        [_timeBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_timeBtn setTitle:@"  22:00前免运费$30免运费,22:00后满$50免运费" forState:UIControlStateNormal];
    }
    return _timeBtn;
}

- (UITextField *)remarksText{
    if (_remarksText == nil) {
        _remarksText = [[UITextField alloc] init];
        _remarksText.placeholder = @"可输入100字以内特殊要求内容";
    }
    return _remarksText;
}
@end
